﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

// 所有系统界面的基类
public abstract class Dialog
{
    protected GameObject _root;

    protected GameObject Init(string path)
    {
           _root = UIManager.Instance.AddUI(path);

        var closeBtn = _root.FindComponent<Button>("BtnClose");
        if (closeBtn != null)
        {
            closeBtn.onClick.AddListener(onBtnClose);
        }

        return _root;
    }

    protected virtual void onBtnClose()
    {
        DialogMgr.Instance.Close(this);
    }

    public void CloseSelf()
    {
        UIManager.Instance.Release(_root);
    }
}

// 所有系统界面的管理类
public class DialogMgr : Singleton<DialogMgr>
{
    List<Dialog> _allDlg = new List<Dialog>();

    // 界面开启
    public T OpenDialog<T>() where T : Dialog, new()
    {
        T dlg = new T();

        _allDlg.Add(dlg);

        return dlg;
    }

    // 界面关闭
    public void Close(Dialog dlg)
    {
        _allDlg.Remove(dlg);
        dlg.CloseSelf();
    }

    // 关闭所有已开启界面
    public void CloseAll()
    {
        foreach (var dlg in _allDlg)
        {
            dlg.CloseSelf();
        }

        _allDlg.Clear();
    }
}
